package io.sage.modules.query.controller;

import com.github.pagehelper.PageHelper;
import io.sage.common.utils.PageUtils;
import io.sage.common.utils.R;
import io.sage.modules.basics.entity.BStatistics;
import io.sage.modules.basics.service.BStatisticsService;
import io.sage.modules.goods.entity.BGoodsRecord;
import io.sage.modules.goods.enums.GoodsStatusEnum;
import io.sage.modules.goods.service.BGoodsRecordService;
import io.sage.modules.goods.service.BGoodsService;
import io.sage.modules.outwarehouse.entity.BEmployeeAccessRecord;
import io.sage.modules.outwarehouse.entity.BGoodsReceiveRecord;
import io.sage.modules.outwarehouse.service.BEmployeeAccessRecordService;
import io.sage.modules.outwarehouse.service.BGoodsReceiveRecordService;
import io.sage.modules.sys.shiro.ShiroUtils;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zxh
 * @Title: AnalysisController
 * @ProjectName sage_wms
 * @Description: TODO
 * @date 2019/7/3014:25
 */
@RestController
@RequestMapping("analysis")
@Api(tags = "首页查询")
public class AnalysisController {

    @Autowired
    private BGoodsReceiveRecordService bGoodsReceiveRecordService;
    @Autowired
    private BEmployeeAccessRecordService bEmployeeAccessRecordService;

    @Autowired
    private BStatisticsService bStatisticsService;

    @Autowired
    private BGoodsService bGoodsService;

    @Autowired
    private BGoodsRecordService bGoodsRecordService;
    /**
     * 按照月查询借入借出记录
     *
     * @return R
     */
    @GetMapping(value = "/lendAndReturn")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {
        //PageUtils page = queryService.queryAll(params);
        //如果 warehouseId 为 null ，返回一个空集合
        if(params==null || params.get("warehouseId")==null) {
            return R.ok().put("list",new ArrayList<>());
        }

        String warehouseId = params.get("warehouseId").toString();

        Map<String,Object>map=new HashMap<>();
        map.put("dateTime","-1");
        map.put("warehouseId", warehouseId);  //添加warehouseId

        PageHelper.startPage(1, 6);
        List<BEmployeeAccessRecord> list= bEmployeeAccessRecordService.selectEmployeeAccessRecordList(map);
        return R.ok().put("list", list);
    }
    /**
     * 按照获取温度
     *
     * @return R
     */
    @GetMapping(value = "/queryReport/{type}")
    @ResponseBody
    public R getTemperature(@PathVariable(value = "type")Integer type) {
        //PageUtils page = queryService.queryAll(params);
        PageHelper.startPage(1, 6);
        BStatistics bStatistics=new BStatistics();
        bStatistics.setIsDeleted(0);
        bStatistics.setType(type);
        List<BStatistics> bStatisticsList= bStatisticsService.queryAll(bStatistics);
        List<List>list=new ArrayList<>();
        List<String>times=new ArrayList<>();
        List<String>value=new ArrayList<>();
        if(bStatisticsList.size()>0){

            for (int i = 0; i < bStatisticsList.size(); i++) {
                bStatistics=bStatisticsList.get(i);

                times.add(bStatistics.getCreateTime().toString());
                value.add(bStatistics.getQuantity().toString());
            }
            Collections.reverse(times);
            Collections.reverse(value);
        }
        list.add(times);
        list.add(value);
        return R.ok().put("list", list);
    }
    /**
     * 按照获取温度
     *
     * @return R
     */
    @GetMapping(value = "/initToDo/{type}/{house}")
    @ResponseBody
    public R initToDo(@PathVariable(value = "type")String type,@PathVariable(value = "house")String house) {
        //PageUtils page = queryService.queryAll(params);
        PageHelper.startPage(1, 6);
        Map<String,Object>map=new HashMap<>();
        List list=null;
       //查询代办
       if("0".equals(type)){
            if(StringUtils.isNotEmpty(house)){
                map.put("wareHouseId", house);
            }
            map.put("goodsStatus", GoodsStatusEnum.WAITCHECK.getCode());
            list= bGoodsService.selectGoodsList(map);
       }//查询已办
       else{
           map.put("userId", ShiroUtils.getUserEntity().getUserId());
           if(StringUtils.isNotEmpty(house)){
               map.put("wareHouseId", house);
           }
           map.put("userId", ShiroUtils.getUserEntity().getUserId());
           list=bGoodsRecordService.queryByMap(map);
       }
        return R.ok().put("list", list);
    }
}
